<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deviceLocation"
            placeholder="请输入设备位置"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="设备编号" prop="deviceCode">
          <el-input
            v-model="queryParams.deviceCode"
            placeholder="请输入设备编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input
            v-model="queryParams.deviceName"
            placeholder="请输入设备名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['wlgl:device:add']"
            >新增</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        </el-form>
        <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
          <el-tree
            ref="tree"
            :data="treeData"
            :props="defaultProps"
            @node-click="handleNodeClick"
            highlight-current
            :expand-on-click-node='isExpandTree'
            node-key="id" />
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="${comment}" align="center" prop="id" />
          <el-table-column label="网关ID" align="center" prop="gatewayId" />
          <el-table-column label="设备编号" align="center" prop="deviceCode" />
          <el-table-column label="设备名称" align="center" prop="deviceName" />
          <el-table-column label="设备状态" align="center" prop="deviceStatus">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.deviceStatus=='0'">正常</el-tag>
              <el-tag type="danger" v-else>异常</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="设备位置" align="center" prop="deviceLocation" />
          <el-table-column label="设备剩余金额" align="center" prop="deviceBalance" >
            <template slot-scope="scope">
            <span v-if="scope.row.deviceStatus=='1' || scope.row.deviceStatus=='2'">
              <span>{{fun(scope.row.deviceBalance)}}</span>
            </span>
              <span v-if="scope.row.deviceStatus=='3' || scope.row.deviceStatus=='4'">
              <span>-</span>
            </span>
          </template>
          </el-table-column>
          <el-table-column label="设备剩余量" align="center" prop="deviceSurplus" />
          <el-table-column label="设备sdk端口号" align="center" prop="deviSdkPort" />
          <el-table-column label="设备出入类型:1进入闸机，2出去闸机" align="center" prop="deviAccessType" />
          <el-table-column label="常开时段" align="center" prop="deviOpenAlwaysTimeIndex" />
          <el-table-column label="常闭时段" align="center" prop="deviCloseAlwaysTimeIndex" />
          <el-table-column label="空间位置" align="center" prop="bsloId" />
          <el-table-column label="设备其他信息" align="center" prop="deviOtherInfo" />
          <el-table-column label="剩余量单位" align="center" prop="surplusUnit" />
          <el-table-column label="水表阀门状态 0-开阀门 1-关阀门" align="center" prop="sluiceStatus" />
          <el-table-column label="电表总状态 0-拉闸 1-合闸" align="center" prop="electricityStatus" />
          <el-table-column label="电表A路状态 0-拉闸 1-合闸" align="center" prop="electricityAStatus" />
          <el-table-column label="电表B路状态 0-拉闸 1-合闸" align="center" prop="electricityBStatus" />
          <el-table-column label="修改时间" align="center" prop="modifyTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.modifyTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="修改人" align="center" prop="modifyBy" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
            <template slot-scope="scope">
              <el-button type="primary"
                         icon="el-icon-edit"
                         @click="handleUpdate(scope.row)"
                         v-hasPermi="['wlgl:device:edit']"
                         circle
              ></el-button>
              <el-button type="danger"
                         icon="el-icon-delete"
                         @click="handleDelete(scope.row)"
                         v-hasPermi="['wlgl:device:remove']"
                         circle
              ></el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>




    <!-- 添加或修改物联设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    <el-row :gutter="30">
      <el-col :span="12">
        <el-form-item label="网关ID" prop="gatewayId">
          <el-input v-model="form.gatewayId" placeholder="请输入网关ID" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="设备编号" prop="deviceCode">
          <el-input v-model="form.deviceCode" placeholder="请输入设备编号" />
        </el-form-item>
      </el-col>
    </el-row>
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="设备名称" prop="deviceName">
              <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备位置" prop="deviceLocation">
              <el-input v-model="form.deviceLocation" placeholder="请输入设备位置" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="设备ip" prop="deviIp">
            <el-input v-model="form.deviIp" placeholder="请输入设备ip" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备用户名" prop="deviUsername">
            <el-input v-model="form.deviUsername" placeholder="请输入设备用户名" />
          </el-form-item>
        </el-col>
      </el-row>
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="设备密码" prop="deviPassword">
              <el-input v-model="form.deviPassword" placeholder="请输入设备密码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">

            <el-form-item label="空间位置" prop="bsloId">
              <el-input v-model="form.bsloId" placeholder="请输入空间位置" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from "@/api/wlgl/device";
import { listLocation } from "@/api/jcsj/location";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import {getLocationpid} from "../../../api/jcsj/location";
export default {
  name: "Device",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      deptOptions: [],
      defaultProps:{
        children: "children",
        label:"locationName"
      },
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      deviceLocation:undefined,
      // 物联设备表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gatewayId: null,
        deviceCode: null,
        deviceType: null,
        deviceName: null,
        deviceStatus: null,
        deviceLocation: [],
        rechargeCount: null,
        deviceBalance: null,
        deviceSurplus: null,
        totle: null,
        deviIp: null,
        deviUsername: null,
        deviPassword: null,
        deviSdkPort: null,
        deviAccessType: null,
        deviOpenAlwaysTimeIndex: null,
        deviCloseAlwaysTimeIndex: null,
        bsloId: null,
        deviOtherInfo: null,
        surplusUnit: null,
        sluiceStatus: null,
        electricityStatus: null,
        electricityAStatus: null,
        electricityBStatus: null,
        modifyTime: null,
        modifyBy: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deviceLocation(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    listLocation(this.queryParams).then(resp=>{
     this.deptOptions= this.handleTree(resp.data, "id", "locationPid");
    })
    this.getList()
  },

  methods: {
    /** 查询物联设备列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });


    },
    fun(val){
      return Number(val).toFixed(2)
    },
    // 筛选节点
    filterNode(value, data) {
      console.log(1111)
      if (!value) return true;
      return data.locationName.indexOf(value) !== -1;
    },

    // 节点单击事件
    handleNodeClick(data) {
      // if(data.locationPid=='0'){
      //   getLocationpid(data.id).then(resp=>{
      //
      //   this.queryParams.deviceLocation=resp.id
      //   })
      // }else {
        this.queryParams.deviceLocation = data.id;
      // }
      this.handleQuery();
    },
    // handleNodeClick (node) {
    //   this.formData.location_id = node.id
    //   this.formData.location_name = node.label
    //   this.$refs.selectRefs.blur() // 收起下拉框
    // },

    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        gatewayId: null,
        deviceCode: null,
        deviceType: null,
        deviceName: null,
        deviceStatus: "0",
        deviceLocation: null,
        rechargeCount: null,
        deviceBalance: null,
        deviceSurplus: null,
        totle: null,
        deviIp: null,
        deviUsername: null,
        deviPassword: null,
        deviSdkPort: null,
        deviAccessType: null,
        deviOpenAlwaysTimeIndex: null,
        deviCloseAlwaysTimeIndex: null,
        bsloId: null,
        deviOtherInfo: null,
        surplusUnit: null,
        sluiceStatus: "0",
        electricityStatus: "0",
        electricityAStatus: "0",
        electricityBStatus: "0",
        createTime: null,
        createBy: null,
        modifyTime: null,
        modifyBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物联设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDevice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物联设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物联设备编号为"' + ids + '"的数据项？').then(function() {
        return delDevice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wlgl/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
